package com.neusoft.neuiotms.dcom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.ProjectDTO;
import com.neusoft.neuiotms.dcom.service.ProjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "项目管理")
@RestController
@RequestMapping(value = "/api/w/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @ApiOperation("查询所有项目")
    @GetMapping
    public ResultListDTO<ProjectDTO> index(final Searchable searchable) {
        return ResultListDTO.success(this.projectService.index(searchable));
    }

    @ApiOperation("分页查询所有项目")
    @GetMapping("/p")
    public PageResultDTO<ProjectDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.projectService.page(searchable, pageable));
    }

    @ApiOperation("创建项目")
    @PostMapping("")
    public ResultDTO<ProjectDTO> create(@RequestBody final ProjectDTO dto) {
        return ResultDTO.success(this.projectService.create(dto));
    }

    @ApiOperation(value = "修改项目")
    @PutMapping("/{id}")
    public ResultDTO<ProjectDTO> update(@PathVariable final Long id,
            @RequestBody final ProjectDTO dto) {
        return ResultDTO.success(this.projectService.update(id, dto));
    }

    @ApiOperation("项目详情")
    @GetMapping("/{id}")
    public ResultDTO<ProjectDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.projectService.detail(id));
    }

    @ApiOperation("根据项目编码查询项目详情")
    @GetMapping("/code/{code}")
    public ResultDTO<ProjectDTO> details(@PathVariable final String code) {
        return ResultDTO.success(this.projectService.getInfoByCode(code));
    }

    @ApiOperation("删除项目")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.projectService.delete(id);
        return ResultDTO.success();
    }
}
